package com.opennews.openplatform.service;

import com.opennews.openplatform.common.constant.ExpenseTypeConstant;
import com.opennews.openplatform.common.constant.IncomeExpenseTypeConstant;
import com.opennews.openplatform.common.constant.IncomeTypeConstant;
import com.opennews.openplatform.entity.tables.records.IncomeExpenseDetailBaseTypeRecord;
import com.opennews.openplatform.jooq.DSLContextWrapper;
import lombok.RequiredArgsConstructor;
import org.jooq.DSLContext;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.opennews.openplatform.entity.Tables.INCOME_EXPENSE_DETAIL_BASE_TYPE;

@RequiredArgsConstructor
@Service
public class IncomeExpenseDetailBaseTypeService {
    private final DSLContext dslContext;
    private final DSLContextWrapper dslContextWrapper;

    /**
     * Prepares all default types.
     */
    public void init() {
        if (dslContextWrapper.count(INCOME_EXPENSE_DETAIL_BASE_TYPE) == 0) {
            List<IncomeExpenseDetailBaseTypeRecord> records = new ArrayList<>();

            records.add(createRecord(IncomeExpenseTypeConstant.EXPENSE, ExpenseTypeConstant.EXPENSE_FAMILY, "日常家庭各类开销", "如超市、用餐、网购等。", 1));
            records.add(createRecord(IncomeExpenseTypeConstant.EXPENSE, ExpenseTypeConstant.EXPENSE_LIVING_COST, "生活成本", "如水电煤、通讯费、宽带等。", 2));
            records.add(createRecord(IncomeExpenseTypeConstant.EXPENSE, ExpenseTypeConstant.EXPENSE_EDUCATION, "教育", "如上课、考试等。", 3));
            records.add(createRecord(IncomeExpenseTypeConstant.EXPENSE, ExpenseTypeConstant.EXPENSE_TRANSPORTATION, "交通", "如乘车、交通卡充值、加油卡充值等。", 4));
            records.add(createRecord(IncomeExpenseTypeConstant.EXPENSE, ExpenseTypeConstant.EXPENSE_SOCIALIZING, "社交", "如各类人情、红包等。", 5));
            records.add(createRecord(IncomeExpenseTypeConstant.EXPENSE, ExpenseTypeConstant.EXPENSE_FAMILY_TRAVEL, "家庭旅行", "包括相关交通费、住宿费、签证费等。", 6));
            records.add(createRecord(IncomeExpenseTypeConstant.EXPENSE, ExpenseTypeConstant.EXPENSE_BUSINESS_TRAVEL, "公务差旅", "比如先行个人垫付的相关差旅费用。", 7));
            records.add(createRecord(IncomeExpenseTypeConstant.EXPENSE, ExpenseTypeConstant.EXPENSE_FAMILY_CAR, "家用车", "如车辆维修、保养、车险等。", 8));
            records.add(createRecord(IncomeExpenseTypeConstant.EXPENSE, ExpenseTypeConstant.EXPENSE_FAMILY_INSURANCE, "家庭保险", "如商业养老、意外、疾病类保险等。", 9));
            records.add(createRecord(IncomeExpenseTypeConstant.EXPENSE, ExpenseTypeConstant.EXPENSE_FAMILY_HEALTH, "家庭健康", "如就医、购药、健身等。", 10));
            records.add(createRecord(IncomeExpenseTypeConstant.EXPENSE, ExpenseTypeConstant.EXPENSE_INVESTMENT, "投资", "如定投、股票、房产等。", 11));
            records.add(createRecord(IncomeExpenseTypeConstant.EXPENSE, ExpenseTypeConstant.EXPENSE_CREDIT_CARD_REPAYMENT, "信用卡还款", "用于信用卡账单还款", 12, false));
            records.add(createRecord(IncomeExpenseTypeConstant.EXPENSE, ExpenseTypeConstant.EXPENSE_PAY_FOR_ANOTHER, "代付", "用于先行代付的公司、他人的费用。", 13, false));
            records.add(createRecord(IncomeExpenseTypeConstant.EXPENSE, ExpenseTypeConstant.EXPENSE_OTHER, "其它", "其它类费用", 14));

            records.add(createRecord(IncomeExpenseTypeConstant.INCOME, IncomeTypeConstant.INCOME_COMPANY_SALARY, "公司薪资收入", 1));
            records.add(createRecord(IncomeExpenseTypeConstant.INCOME, IncomeTypeConstant.INCOME_COMPANY_BONUS, "公司奖金收入", 2));
            records.add(createRecord(IncomeExpenseTypeConstant.INCOME, IncomeTypeConstant.INCOME_COMPANY_REIMBURSEMENT, "公司报销", 3, false));
            records.add(createRecord(IncomeExpenseTypeConstant.INCOME, IncomeTypeConstant.INCOME_COMPANY_OTHER_INCOME, "公司其它收入", 4));
            records.add(createRecord(IncomeExpenseTypeConstant.INCOME, IncomeTypeConstant.INCOME_INVESTMENT_RETURN, "投资回报", 5));
            records.add(createRecord(IncomeExpenseTypeConstant.INCOME, IncomeTypeConstant.INCOME_REFUND, "退款", 6));
            records.add(createRecord(IncomeExpenseTypeConstant.INCOME, IncomeTypeConstant.INCOME_PAY_FOR_ANOTHER_REPAYMENT, "代付回款", 7, false));
            records.add(createRecord(IncomeExpenseTypeConstant.INCOME, IncomeTypeConstant.INCOME_OTHER, "其它收入", 8));

            dslContext.batchInsert(records);
        }
    }

    private IncomeExpenseDetailBaseTypeRecord createRecord(String incomeExpenseType, String name, String title, String subtitle, int orderIndex, boolean analysisIncluded) {
        IncomeExpenseDetailBaseTypeRecord record = dslContext.newRecord(INCOME_EXPENSE_DETAIL_BASE_TYPE);
        record.setIncomeExpenseType(incomeExpenseType);
        record.setName(name);
        record.setTitle(title);
        record.setSubtitle(subtitle);
        record.setOrderIndex(orderIndex);

        return record;
    }

    private IncomeExpenseDetailBaseTypeRecord createRecord(String incomeExpenseType, String name, String title, String subtitle, int orderIndex) {
        return createRecord(incomeExpenseType, name, title, subtitle, orderIndex, true);
    }

    private IncomeExpenseDetailBaseTypeRecord createRecord(String incomeExpenseType, String name, String title, int orderIndex) {
        return createRecord(incomeExpenseType, name, title, null, orderIndex, true);
    }

    private IncomeExpenseDetailBaseTypeRecord createRecord(String incomeExpenseType, String name, String title, int orderIndex, boolean analysisIncluded) {
        return createRecord(incomeExpenseType, name, title, null, orderIndex, analysisIncluded);
    }
}
